<style>
  .companyFormPhotos { 
       
    /* margin-left:140px; */
    line-height: 38px;
    float: left;
  }
  .companyFormPhotos img {
    cursor: pointer;
    height: 38px;
    width: auto;
    max-width: 200px;
  }
  #warehouseOrderForm .layui-form-label {
    width: 110px;
  }

  #warehouseOrderForm .layui-input-block {
    margin-left: 140px;
  }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">订单编号</label>
                  <div class="layui-input-inline mr0">
                    <input
                      name="orderCode"
                      class="layui-input"
                      type="text"
                      placeholder="请输入产品名称"
                      autocomplete="off"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <button
                    class="layui-btn icon-btn color-green"
                    lay-filter="formSubSearchLog"
                    lay-submit
                    layui-form-keyDownSearch="true"
                  >
                    查询
                  </button>
                  <button
                    class="layui-btn icon-btn color-white"
                    lay-filter="resets"
                    lay-submit
                  >
                    重置
                  </button>
                </div>
              </div>
              <div class="query-rt"></div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button
                id="warehouseOrderBtnAdd"
                class="layui-btn icon-btn color-reseda"
              >
                添加
              </button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table
        class="layui-table"
        id="warehouseOrderTable"
        lay-filter="warehouseOrderTable"
      ></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseOrderTableBar">
  <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="info">详情</a>
  {{# if(d.status == 1){}}
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="accept">受理</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="withdraw"
    >撤销</a
  >
  {{# }}}
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="warehouseOrderForm">
  <form lay-filter="warehouseOrderForm" class="layui-form model-form">
    <div class="open-auto">
      <div class="layui-form-item layui-row">
        <input name="id" type="hidden" />
        <div class="layui-col-xs3">
          <label class="layui-form-label">托管客户</label>
          <div class="layui-input-block">
            <input type="hidden" id="customerName" name="customerName" />
            <select
              id="customerNameId"
              name="customerNameId"
              lay-filter="customerNameId"
              lay-verify="required"
              lay-search
              required
            ></select>
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label">客户代码</label>
          <div class="layui-input-block">
            <input
              id="customerId"
              name="customerId"
              type="text"
              class="layui-input"
              disabled
            />
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label">联系人</label>
          <div class="layui-input-block">
            <input type="hidden" id="warehouseUserId" name="warehouseUserId" />
            <select
              id="userNameId"
              name="userNameId"
              lay-filter="userNameId"
              lay-verify="required"
            ></select>
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label">电话</label>
          <div class="layui-input-block">
            <input id="phone" name="phone" type="text" class="layui-input" />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <!--                <div class="layui-col-xs3">-->
        <!--                    <label class="layui-form-label">邮箱</label>-->
        <!--                    <div class="layui-input-block">-->
        <!--                        <input id="mail" name="mail" type="text" class="layui-input" />-->
        <!--                    </div>-->
        <!--                </div>-->

        <div class="layui-col-xs3">
          <label class="layui-form-label">预计入库日期</label>
          <div class="layui-input-block">
            <input
              type="text"
              class="layui-input"
              id="storeTime"
              name="storeTime"
              placeholder="请选择预计入库时间"
              readonly
            />
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label">预计计费日期</label>
          <div class="layui-input-block">
            <input
              type="text"
              class="layui-input"
              id="chargingTime"
              name="chargingTime"
              placeholder="请选择预计计费时间"
              readonly
            />
          </div>
        </div>

        <div class="layui-col-xs3">
          <label class="layui-form-label">合计重量 </label>
          <div class="layui-input-block">
            <input
              name="totalWeight"
              id="totalWeight"
              class="layui-input"
              disabled
            />
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label">合计数量 </label>
          <div class="layui-input-block">
            <input
              type="hidden"
              name="totalPrice"
              id="totalPrice"
              class="layui-input"
              disabled
            />
            <input
              name="totalQuantity"
              id="totalQuantity"
              class="layui-input"
              disabled
            />
          </div>
        </div>
      </div>
      <div class="layui-form-item layui-row">
        <!--                <div class="layui-col-xs3" >-->
        <!--                    <label class="layui-form-label">预计计费日期</label>-->
        <!--                    <div class="layui-input-block">-->
        <!--                        <input type="text" class="layui-input" id="chargingTime" name="chargingTime" placeholder="请选择预计计费时间" readonly >-->
        <!--                    </div>-->
        <!--                </div>-->
      </div>
      <div class="layui-form-item tableContent">
        <button class="layui-btn" type="button" id="btnAddWarehouseProduct">
          添加物料
        </button>
        <table
          class="layui-table"
          id="warehouseProductFormTable"
          lay-filter="warehouseProductFormTable"
        ></table>
      </div>
      <div class="layui-form-item text-right">
        <button
          class="layui-btn layui-btn-primary"
          type="button"
          ew-event="closeDialog"
        >
          返回
        </button>
        <button class="layui-btn" type="button" id="btnAcceptance" lay-submit>
          受理
        </button>
        <button
          class="layui-btn"
          id="warehouseOrderFormSubmit"
          lay-filter="warehouseOrderFormSubmit"
          lay-submit
        >
          保存
        </button>
      </div>
    </div>
  </form>
</script>
<script type="text/html" id="warehouseProductFormTableBar">
  <button
    class="layui-btn layui-btn-danger layui-btn-xs"
    lay-event="del"
    type="button"
  >
    删除
  </button>
</script>

<!-- js部分 -->
<script>
  layui.use(
    [
      "layer",
      "form",
      "table",
      "admin",
      "upload",
      "tableTreeDj",
      "config",
      "laydate",
    ],
    function () {
      var $ = layui.jquery;
      var layer = layui.layer;
      var form = layui.form;
      var table = layui.table;
      var config = layui.config;
      var admin = layui.admin;
      var upload = layui.upload;
      var tableTree = layui.tableTreeDj;
      var laydate = layui.laydate;

      // 渲染表格
      var insTb = table.render({
        elem: "#warehouseOrderTable",
        url: config.base + "warehouse/warehouse/order/list",
        toolbar: true,
        defaultToolbar: ["filter"],
        page: true,
        cellMinWidth: 100,
        cols: [
          [
            { type: "numbers", title: "序号", fixed: "left" },
            { field: "orderCode", title: "订单编号12", align: "center" },
            { field: "sourceDesc", title: "数据来源", align: "center" },
            // ,{field: 'totalQuantity', title: '物料数量', align: 'center'}
            { field: "rowsd", title: "物料数量", align: "center" },
            // ,{field: 'customerId', title: '客户代码' , align: 'center'}
            { field: "code", title: "客户代码", align: "center" },
            { field: "warehouseUser", title: "联系人", align: "center" },
            // ,{field: 'totalPrice', title: '托管单价' , align: 'center'}
            { field: "totalWeight", title: "订单物料重量", align: "center" },
            { field: "storeTime", title: "计划入库时间", align: "center" },
            { field: "chargingTime", title: "计划计费时间", align: "center" },
            {
              field: "status",
              title: "状态",
              align: "center",
              templet: function (d) {
                // 根据 status 的值返回对应的显示内容 0已撤销1.待受理2.已受理3.待发货4.已发货5.签收完成6.入库完成
                switch (d.status) {
                  case 0:
                    return '<span style="color: #999;">已撤销</span>'; // 灰色
                  case 1:
                    return '<span style="color: #999;">待受理</span>'; // 灰色
                  case 2:
                    return '<span style="color: #5FB878;">已受理</span>'; // 绿色
                  case 3:
                    return '<span style="color: #FF5722;">待发货</span>';
                  case 4:
                    return '<span style="color: #FF5722;">已发货</span>';
                  case 5:
                    return '<span style="color: #FF5722;">签收完成</span>'; // 红色
                  case 6:
                    return '<span style="color: #FF5722;">入库完成</span>'; // 红色
                  default:
                    return "未知状态";
                }
              },
            },
            { field: "createTime", title: "创建时间", align: "center" },
            // ,{field: 'creator', title: '创建人' , align: 'center'}
            { field: "nickname", title: "创建人", align: "center" },
            {
              align: "center",
              toolbar: "#warehouseOrderTableBar",
              title: "操作",
              width: 200,
              fixed: "right",
            },
          ],
        ],
        done: function (res, curr, count) {
          //移除按钮
          // var menuButton = config.getMenuButton(location.hash);
          // if(menuButton.indexOf("搜索") == -1){
          //     $('.layui-form.toolbar').remove();
          // }
          // if(menuButton.indexOf("修改") == -1){
          //     $("[lay-event='edit']").remove();
          // }
          // if(menuButton.indexOf("删除") == -1){
          //     $("[lay-event='del']").remove();
          // }
        },
      });

      // 工具条点击事件
      table.on("tool(warehouseOrderTable)", function (obj) {
        var data = obj.data;
        if (obj.event === "info") {
          //详情
          info(data);
          //处理详情数据进行包装
        } else if (obj.event === "accept") {
          // 受理
          acceptance(data);
          // updateStatus(data,2);
        } else if (obj.event === "withdraw") {
          // 撤回
          updateStatus(data, 0);
        }
      });
      function acceptance(data) {
        admin.req(
          "warehouse/warehouse/order/info",
          { id: data.id },
          function (res) {
            if (res.code === 200) {
              let data = res.data;
              data.customerNameId = res.data.customerId;
              data.userNameId = res.data.warehouseUserId;
              data.status = 2;
              data.isaccptance = 1;
              showEditModel(data);
            }
          }
        );
      }

      function info(data) {
        admin.req(
          "warehouse/warehouse/order/info",
          { id: data.id },
          function (res) {
            if (res.code === 200) {
              let data = res.data;
              data.customerNameId = res.data.customerId;
              data.userNameId = res.data.warehouseUserId;
              data.isaccptance = 2;
              showEditModel(data);
            }
          }
        );
      }
      // 修改订单状态
      function updateStatus(data, status) {
        admin.req(
          "warehouse/warehouse/order/updateStatus",
          { id: data.id, status: status },
          function (res) {
            if (res.code == 200) {
              layer.msg(res.msg, { icon: 1 });
              table.reload("warehouseOrderTable", {});
            } else {
              layer.msg(res.msg, { icon: 2 });
            }
          },
          "GET"
        );
        layer.close(index);
      }

      //监听排序
      table.on("sort(warehouseOrderTable)", function (obj) {
        table.reload("warehouseOrderTable", {
          initSort: obj,
          where: {
            sort: obj.field,
            order: obj.type,
          },
        });
      });

      // 搜索
      form.on("submit(formSubSearchLog)", function (data) {
        insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
      });

      // 重置搜索
      form.on("submit(resets)", function (data) {
        insTb.reload(
          { where: admin.resetSearch(data.field), page: { curr: 1 } },
          "data"
        );
      });

      // 添加按钮点击事件
      $("#warehouseOrderBtnAdd").click(function () {
        showEditModel();
      });

      // 显示编辑弹窗
      function showEditModel(data) {
        //debugger;
        // if(data==undefined){
        //     data.isaccptance=3;
        // }
        admin.formOpen({
          type: 1,
          area: ["90%", "80%"],
          offset: "65px",
          title: data ? "托管订单详情" : "添加托管订单",
          content: $("#warehouseOrderForm").html(),
          success: function () {
            //客户信息
            admin.reqSync(
              "enterprise/sell/admin/customersInfo/list",
              { limit: 9999 },
              function (res) {
                layer.closeAll("loading");
                if (res.code == 200) {
                  loadSelectHtml(res.data, "请选客户信息", "customerNameId", 1);
                }
              },
              "GET"
            );
            //设备型号下拉操作
            form.on("select(customerNameId)", function (data) {
              if (data.value === "") {
              } else {
                let dataObj = JSON.parse(
                  $(data.elem[data.elem.selectedIndex]).attr("data")
                );
                $("#customerId").val(dataObj.id);
                userNameSelect(dataObj);
              }
            });
            function userNameSelect(data) {
              admin.reqSync(
                "enterprise/sell/admin/contactInfo/list?page=1&limit=9999",
                { selCustomerId: data ? data.id : null },
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    loadSelectHtml(res.data, "请选客户信息", "userNameId", 2);
                  }
                },
                "GET"
              );
            }
            form.on("select(userNameId)", function (data) {
              if (data.value === "") {
              } else {
                let dataObj = JSON.parse(
                  $(data.elem[data.elem.selectedIndex]).attr("data")
                );
                $("#warehouseUserId").val(dataObj.id);
                $("#phone").val(dataObj.telephone);
                $("#mail").val(dataObj.email);
              }
            });

            parentData.data = data;
            const nowData = { ...parentData };
            // 查看大图
            layer.photos({
              photos: ".companyFormPhotos",
              anim: 5,
            });
            laydate.render({
              elem: "#storeTime",
              type: "datetime",
            });
            laydate.render({
              elem: "#chargingTime",
              type: "datetime",
            });
            table.render({
              elem: "#warehouseProductFormTable",
              page: false,
              limit: 999999,
              height: 400,
              data: [],
              cols: [
                [
                  { type: "numbers", title: "序号", fixed: "left" },
                  {
                    field: "productCode",
                    title: "客户物料料号",
                    align: "center",
                  },
                  {
                    field: "productMaterial",
                    title: "物料类别",
                    align: "center",
                  },
                  {
                    field: "productProducer",
                    title: "生产厂家",
                    align: "center",
                  },
                  { field: "productBrand", title: "品牌", align: "center" },
                  { field: "productMainUnit", title: "单位", align: "center" },
                  {
                    field: "quantity",
                    align: "center",
                    title: "托管数量",
                    templet: admin.setTableInput(
                      "quantity",
                      nowData.eventName === "view",
                      "quantity"
                    ),
                  },
                  {
                    field: "netWeight",
                    align: "center",
                    title: "单位净重(吨)",
                    // ,
                    // templet: admin.setTableInput('netWeight', nowData.eventName === 'view', 'netWeight'),
                  },
                  {
                    field: "grossWeight",
                    align: "center",
                    title: "单位毛重(吨)",
                    // ,
                    // templet: admin.setTableInput('grossWeight', nowData.eventName === 'view', 'grossWeight'),
                  },
                  {
                    field: "totalWeight",
                    align: "center",
                    title: "合计重量",
                    templet: function (data) {
                      // 修改操作：计算 totalPrice 和 totalWeight 的和
                      if (
                        data &&
                        data.quantity !== undefined &&
                        data.grossWeight !== undefined
                      ) {
                        var quantity = Number(data.quantity) || 0;
                        var netWeight = Number(data.netWeight) || 0;
                        return quantity * netWeight;
                      }
                      // 添加操作：返回原始值
                      return data.totalWeight || "";
                    },
                  },
                  {
                    field: "warehousePrice",
                    align: "center",
                    title: "托管单价(天)",
                  },
                  {
                    field: "price",
                    align: "center",
                    title: "小计金额",
                    templet: function (data) {
                      // 修改操作：计算 totalPrice 和 totalWeight 的和
                      if (
                        data &&
                        data.quantity !== undefined &&
                        data.warehousePrice !== undefined
                      ) {
                        var quantity = Number(data.quantity) || 0;
                        var warehousePrice = Number(data.warehousePrice) || 0;
                        var netWeight = Number(data.netWeight) || 0;
                        return quantity * warehousePrice * netWeight;
                      }
                      // 添加操作：返回原始值
                      return data.warehousePrice || "";
                    },
                  },
                  {
                    align: "center",
                    fixed: "right",
                    toolbar: "#warehouseProductFormTableBar",
                    title: "操作",
                    hide: nowData.eventName === "view",
                  },
                ],
              ],
              done: function () {
                admin.tableInputChangeByClass(
                  "warehouseProductFormTable",
                  "quantity",
                  (index, value, tableData, field, tableId) => {
                    //
                    let quantity = tableData[index].quantity;
                    let weight = tableData[index].netWeight;
                    let warehousePrice = tableData[index].warehousePrice;
                    // if(quantity<1){
                    //     layer.msg('托管数量不能为负数', { icon: 2 }); // 提示用户
                    //     return false; // 阻止表单提交
                    // }
                    tableData[index].price =
                      Number(quantity) *
                      Number(weight) *
                      Number(warehousePrice);
                    tableData[index].totalWeight =
                      Number(quantity) * Number(weight);
                    admin.setTableColHtml(
                      "warehouseProductFormTable",
                      index,
                      "price",
                      tableData[index].price
                    );
                    admin.setTableColHtml(
                      "warehouseProductFormTable",
                      index,
                      "totalWeight",
                      tableData[index].totalWeight
                    );
                    $("#totalWeight").val(
                      tableData.reduce((total, item) => {
                        return total + Number(item.totalWeight);
                      }, 0)
                    );
                    $("#totalQuantity").val(
                      tableData.reduce((total, item) => {
                        return total + Number(item.quantity);
                      }, 0)
                    );
                    $("#totalPrice").val(
                      tableData.reduce((total, item) => {
                        return total + Number(item.price);
                      }, 0)
                    );
                  }
                );
                admin.tableInputChangeByClass(
                  "warehouseProductFormTable",
                  "grossWeight",
                  (index, value, tableData, field, tableId) => {
                    let quantity = tableData[index].quantity;
                    let weight = tableData[index].netWeight;
                    let warehousePrice = tableData[index].warehousePrice;
                    tableData[index].price =
                      Number(quantity) *
                      Number(weight) *
                      Number(warehousePrice);
                    tableData[index].totalWeight =
                      Number(quantity) * Number(weight);
                    admin.setTableColHtml(
                      "warehouseProductFormTable",
                      index,
                      "price",
                      tableData[index].price
                    );
                    admin.setTableColHtml(
                      "warehouseProductFormTable",
                      index,
                      "totalWeight",
                      tableData[index].totalWeight
                    );
                    $("#totalWeight").val(
                      tableData.reduce((total, item) => {
                        return total + Number(item.totalWeight);
                      }, 0)
                    );
                    $("#totalQuantity").val(
                      tableData.reduce((total, item) => {
                        return total + Number(item.quantity);
                      }, 0)
                    );
                    $("#totalPrice").val(
                      tableData.reduce((total, item) => {
                        return total + Number(item.price);
                      }, 0)
                    );
                  }
                );
                admin.tableInputChangeByClass(
                  "warehouseProductFormTable",
                  "netWeight",
                  {}
                );
              },
              error: function () {
                console.log("请求失败");
              },
            });

            if (data) {
              userNameSelect(data.customerNameId);
              table.reload("warehouseProductFormTable", {
                data: data.orderInfoDetail, // 设置新的数据
              });
            }
            form.val("warehouseOrderForm", data);

            // 表单提交事件
            form.on("submit(warehouseOrderFormSubmit)", function (d) {
              var quantityInputs = document.querySelectorAll(
                'input[data-field="quantity"]'
              ); // 获取所有 quantity 输入框
              var isValid = true;
              var isNegative = true;
              debugger;
              // 遍历所有输入框
              quantityInputs.forEach(function (input) {
                debugger;
                if (!input.value.trim()) {
                  isValid = false; // 如果某一行的 quantity 为空，标记为无效
                }
                if (input.value < 1) {
                  isNegative = false; //如果有某列为负数
                }
              });

              // 校验结果
              if (!isValid) {
                layer.msg("托管数量不能为空", { icon: 2 }); // 提示用户
                return false; // 阻止表单提交
              }

              if (!isNegative) {
                layer.msg("托管数量不能为0或负数", { icon: 2 }); // 提示用户
                return false; // 阻止表单提交
              }

              const formData = { ...d.field };
              formData.productOrderDetail =
                table.cache["warehouseProductFormTable"];
              layer.load(2);
              admin.req(
                data
                  ? "warehouse/warehouse/order/update"
                  : "warehouse/warehouse/order/add",
                JSON.stringify(formData),
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    table.reload("warehouseOrderTable", {});
                    layer.closeAll("page");
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                data ? "PUT" : "POST",
                false,
                "application/json"
              );
            });
            //受理按钮修改状态
            $("#btnAcceptance").click(function () {
              // debugger;
              var chargingTime = $("#chargingTime").val();
              if (chargingTime == null || chargingTime == "") {
                layer.msg("请选择计费日期", { icon: 2 });
                return false;
              }
              admin.req(
                "warehouse/warehouse/order/acceptance",
                { id: data.id, chargingTime: chargingTime },
                function (res) {
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    table.reload("warehouseOrderTable", {});
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                "GET"
              );
              // layer.close(index);
              layer.closeAll("page");
            });

            $("#btnAddWarehouseProduct").click(function () {
              admin.formOpen({
                area: "90%",
                offset: "50px",
                title: "配置物料信息",
                path: "components/warehouse/product/productListForm.html",
                //   path: "components/warehouse/product/product.html",
                success: function () {
                  parentData.eventName = "checkbox";
                  parentData.cacheList = [
                    ...table.cache["warehouseProductFormTable"],
                  ];
                  parentData.customerId = $("#customerNameId").val();
                  console.log(parentData.cacheList);
                  if (parentData.cacheList.length > 0) {
                    parentData.notInListIds = table.cache[
                      "warehouseProductFormTable"
                    ]
                      .map((v) => {
                        if (v.productDetailId) {
                          return v.productDetailId;
                        }
                        if (v.id) {
                          return v.id;
                        }
                      })
                      .join(",");
                  } else {
                    parentData.notInListIds = "";
                  }
                },
                end: function () {
                  if (childData.list) {
                    const newData = childData.list || [];
                    newData.forEach((v) => {
                      v.productCategory = v.productTypeId;
                      v.productName = v.skuName;
                      v.specifications = v.skuSpec;
                      v.unitName = v.mainUnitName;
                      v.remark = "";
                      v.rate = v.taxRate === "" || !v.taxRate ? "0" : v.taxRate;
                      v.taxPrice = v.currentCostPrice;
                    });
                    const oldData = table.cache["warehouseProductFormTable"];
                    const list = [...oldData, ...newData]
                  //    .map((v) => {
                  //      v.warehousePrice = v.price;
                  //      delete v.price;
                  //      return v;
                  //    });
                    table.reload("warehouseProductFormTable", {
                      data: list, // 设置新的数据
                    });
                  }
                },
              });
            });
            table.on("tool(warehouseProductFormTable)", function (obj) {
              var d = obj.data; // 获取当前行的数据
              var layEvent = obj.event; // 获取事件名（edit 或 del）
              if (obj.tr) {
                obj.tr.on("click", function (e) {
                  e.preventDefault(); // 防止默认行为
                });
              }
              if (layEvent == "del") {
                const index = $(obj.tr[0]).data("index");
                let list = table.cache["warehouseProductFormTable"].filter(
                  (v) => {
                    return v["LAY_TABLE_INDEX"] != index;
                  }
                );
                table.reload("warehouseProductFormTable", {
                  data: list,
                });
              }
            });
          },
        });
        if (data == undefined) {
          $("#btnAcceptance").hide(); // 隐藏受理按钮
          $("#warehouseOrderFormSubmit").show();
        } else {
          if (data.isaccptance == 1) {
            $("#btnAcceptance").show(); // 显示受理按钮
            $("#warehouseOrderFormSubmit").hide(); // 隐藏保存按钮
            $("#btnAddWarehouseProduct").hide(); // 隐藏保存按钮
            setReadOnlyTime();
          } else if (data.isaccptance == 2) {
            setReadOnly();
            $("#btnAcceptance").hide();
            $("#warehouseOrderFormSubmit").hide();
            $("#btnAddWarehouseProduct").hide();
          } else {
            $("#btnAcceptance").hide(); // 隐藏受理按钮
            $("#warehouseOrderFormSubmit").show();
          }
        }

        function setReadOnly() {
          // 获取所有输入框
          var inputs = document.querySelectorAll("input");
          inputs.forEach(function (input) {
            input.readOnly = true; // 设置为只读
            input.style.backgroundColor = "#f2f2f2"; // 可选：设置背景色
          });

          // 获取所有下拉框
          var selects = document.querySelectorAll("select");
          selects.forEach(function (select) {
            select.disabled = true; // 设置为禁用
            select.style.backgroundColor = "#f2f2f2"; // 可选：设置背景色
          });
          // 获取所有删除按钮（lay-event="del"）
          var deleteButtons = document.querySelectorAll(
            'button[lay-event="del"]'
          );
          deleteButtons.forEach(function (button) {
            button.style.display = "none"; // 设置为隐藏
          });
          // layui.layer.msg('已设置所有输入框和下拉框为只读');
        }

        function setReadOnlyTime() {
          // 获取所有输入框
          var inputs = document.querySelectorAll("input");
          inputs.forEach(function (input) {
            // 排除 id 为 chargingTime 的输入框
            if (input.id !== "chargingTime") {
              input.readOnly = true; // 设置为只读
              input.style.backgroundColor = "#f2f2f2"; // 可选：设置背景色
            }
          });

          // 获取所有下拉框
          var selects = document.querySelectorAll("select");
          selects.forEach(function (select) {
            select.disabled = true; // 设置为禁用
            select.style.backgroundColor = "#f2f2f2"; // 可选：设置背景色
          });

          // 获取所有删除按钮（lay-event="del"）
          var deleteButtons = document.querySelectorAll(
            'button[lay-event="del"]'
          );
          deleteButtons.forEach(function (button) {
            button.style.display = "none"; // 设置为隐藏
          });

          // layui.layer.msg('已设置所有输入框和下拉框为只读（排除特定输入框）');
        }
      }

      // option渲染
      function loadSelectHtml(data, tips, htmltarget, type) {
        let html = " <option value>" + tips + "</option>";
        if (!data || data == null) {
          return;
        }
        for (let i = 0; i < data.length; i++) {
          if (type == 1) {
            html += `<option value='${data[i].id}' id='${
              data[i].id
            }' data='${JSON.stringify(data[i])}' >${
              data[i].customerName
            }</option>`;
          } else if (type == 2) {
            html += `<option value='${data[i].id}' id='${
              data[i].id
            }' data='${JSON.stringify(data[i])}'>${data[i].name}</option>`;
          } else if (type == 3) {
            html += `<option value='${data[i].id}' id='${data[i].id}'>${data[i].producerName}</option>`;
          } else {
            html += `<option value='${data[i].id}' data='${JSON.stringify(
              data[i]
            )}'>${data[i].name}</option>`;
          }
        }

        $("form select[name='" + htmltarget + "']").html(html);
        form.render("select");
      }

      // 上传图片
      function uploadImg(_elem, pathId, inputId) {
        upload.render({
          elem: "#" + _elem,
          url: config.upload_url + "upload",
          data: {},
          accept: "file",
          exts: "jpg|png|RAR|zip|pdf|txt|doc|docx|xls|xlsx|ppt|pptx",
          headers: { Authorization: "Bearer " + config.getToken() },
          done: function (res) {
            if (res.code == 200) {
              layer.msg(res.msg || "上传成功", { icon: 1 });
              let imagUrl = res.data.url;
              $("#" + pathId).html('<img src="' + imagUrl + '"/>');
              $("#" + inputId).val(imagUrl);
            } else if (res.code == 502) {
              return location.replace("member/login.html");
            } else {
              layer.msg(res.msg || "上传失败", { icon: 2 });
            }
          },
        });
      }
    }
  );
</script>
